/* 
 *   Copyright (c) 2021 Liu Bingyi
 *   MineSweeping is licensed under Mulan PSL v2.
 *   You can use this software according to the terms and conditions of the Mulan PSL v2.
 *   You may obtain a copy of Mulan PSL v2 at:
 *            http://license.coscl.org.cn/MulanPSL2
 *   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *   EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 *   MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *   See the Mulan PSL v2 for more details.
 * 
 *   Author: LiuBingyi
 *   Email: cecillearofa@163.com
 *   Created at 2021/03/24 22:12:59
 */
package minesweeping;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;

public class MineSweepingBoard {

    private int boardRow;
    private int boardColumn;
    private int boardMine;
    private int[][] boardMap;
    private Box[][] boardGrid;

    /** 表示地雷的信号量 */
    public static final int SIGNMINE = 9;
    /** 表示空格的信号量 */
    public static final int SIGNSPACE = 0;

    public MineSweepingBoard(int row, int column, int mine) {
        boardRow = row;
        boardColumn = column;
        boardMine = mine;

        boardMap = new int[boardRow][boardColumn];
        boardGrid = new Box[boardRow][boardColumn];
        for (int i = 0; i < boardRow; i++) {
            for (int j = 0; j < boardColumn; j++) {
                boardGrid[i][j] = new Box();
            }
        }
        initBoard();
    }

    /**
     * 初始化整个棋盘
     */
    private void initBoard() {
        putMine();
    }

    /**
     * 放置地雷
     */
    private void putMine() {
        Random rand;
        try {
            // 获取一个随机数,其决定放置第一个地雷的位置
            rand = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
            noSuchAlgorithmException.printStackTrace();
            rand = new Random(20);
        } catch (Exception e) {
            e.printStackTrace();
            rand = new Random(20);
        }

        int currentRow = 0;
        int currentColumn = 0;
        for (int i = 0; i < boardMine; i++) {
            currentColumn += rand.nextInt(10);
            if (currentColumn >= boardColumn) {
                currentColumn %= boardColumn;
                currentRow++;
            }
            boardMap[currentRow][currentColumn] = SIGNMINE;
            putNum(currentRow, currentColumn);
            currentColumn++;
        }
        boardGridFresh();
    }

    /**
     * 放置地雷旁边的数字
     */
    private void putNum(int currentRow, int currentColumn) {

        for (int i = currentRow - 1; i <= currentRow + 1; i++) {
            for (int j = currentColumn - 1; j <= currentColumn + 1; j++) {
                if (i >= 0 && j >= 0 && i < boardRow && j < boardColumn && boardMap[i][j] != SIGNMINE) {
                    boardMap[i][j] += 1;
                }
            }
        }

    }

    /**
     * 清零整个棋盘,然后重新初始化
     */
    public void instanceFresh() {
        for (int i = 0; i < boardRow; i++) {
            for (int j = 0; j < boardColumn; j++) {
                boardMap[i][j] = SIGNSPACE;
            }
        }
        initBoard();
    }

    /**
     * 刷新整个棋盘，并将所有box置为关闭状态
     */
    public void boardGridFresh() {
        for (int i = 0; i < boardRow; i++) {
            for (int j = 0; j < boardColumn; j++) {
                boardGrid[i][j].setInner(boardMap[i][j]);
                boardGrid[i][j].closeBox();

            }
        }
    }

    public int[][] getBoardMap() {
        return boardMap.clone();
    }

    public Box[][] getBoardGrid() {
        if (boardGrid == null) {
            boardGridFresh();
        }
        return boardGrid;
    }

    public int getBoardColumn() {
        return boardColumn;
    }

    public int getBoardRow() {
        return boardRow;
    }

    public int getBoardMine() {
        return boardMine;
    }

}